package com.adan.content.controller;

import com.adan.base.execption.XcPlusException;
import com.adan.base.model.PageParams;
import com.adan.base.model.PageResult;
import com.adan.base.valid.ValidationGroups;
import com.adan.content.model.dto.AddCourseDto;
import com.adan.content.model.dto.CourseBaseInfoDto;
import com.adan.content.model.dto.EditCourseDto;
import com.adan.content.model.dto.QueryCourseParamsDto;
import com.adan.content.model.po.CourseBase;
import com.adan.content.service.CourseBaseService;
import com.adan.content.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author adan
 */
@Slf4j
@RestController
@RequestMapping("course")
@Api(value = "课程信息编辑接口",tags = "课程信息编辑接口")
public class CourseBaseController {

    @Autowired
    private CourseBaseService courseBaseService;

    @ApiOperation("修改课程基础信息")
    @PutMapping()
    public CourseBaseInfoDto updateCourseBase(@Validated(ValidationGroups.Update.class)
                                                  @RequestBody EditCourseDto editCourseDto){
        //机构id，由于认证系统没有上线暂时硬编码
        Long companyId = 1232141425L;
        return courseBaseService.updateCourseBase(companyId, editCourseDto);
    }

    @ApiOperation("根据课程id查询课程基础信息")
    @GetMapping("/{courseId}")
    public CourseBaseInfoDto queryCourseBaseById(@PathVariable Long courseId){
        //取出当前用户身份。底层使用的ThreadLocal，把用户身份信息放到线程里
        // Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // System.out.println(principal);
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        System.out.println(user);
        return courseBaseService.queryCourseBaseById(courseId);
    }


    @ApiOperation("课程查询接口")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    @PostMapping("/list")
    public PageResult<CourseBase> list(PageParams pageParams,
                                       @RequestBody QueryCourseParamsDto queryCourseParamsDto){
        //取出用户身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        //机构id
        String companyId = user.getCompanyId();
        if (companyId == null){
            XcPlusException.cast("无权限操作");
        }
        return courseBaseService.queryCourseBaseList(Long.parseLong(companyId), pageParams, queryCourseParamsDto);

    }

    @ApiOperation("新增课程基础信息")
    @PostMapping()
    public CourseBaseInfoDto createCourseBase(@Validated(ValidationGroups.Insert.class)
                                                  @RequestBody AddCourseDto courseDto){
        Long companyId = 1232141425L;
        return courseBaseService.createCourseBase(companyId, courseDto);
    }

    @ApiOperation("删除课程")
    @DeleteMapping("/course/{courseId}")
    public void deleteCourse(@PathVariable Long courseId) {
        Long companyId = 1232141425L;
        courseBaseService.removeCourse(companyId,courseId);
    }


}
